package com.maokeyang.messageboard.controller;

import com.maokeyang.messageboard.mapper.UserMapper;
import com.maokeyang.messageboard.model.User;
import com.maokeyang.messageboard.service.UserService;
import com.maokeyang.messageboard.util.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
public class IndexController {

    @Value("${spring.application.name}")
    private String projectName;

    @Autowired
    private UserService userService;

    /**
     * 登录页面
     *
     * @return
     */
    @GetMapping({"/", "/login.html"})
    public String index(Model model) {
        model.addAttribute("projectName", projectName);
        return "login";
    }

    /**
     * 注册页面
     *
     * @return
     */
    @GetMapping({"/register", "/register.html"})
    public String register() {
        return "register";
    }


    /**
     * 用户登录
     *
     * @param mobile
     * @param password
     * @param request
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestParam("mobile") String mobile,
                        @RequestParam("password") String password,
                        HttpServletRequest request,
                        RedirectAttributes attr) {

        if (!CodeUtil.checkVerifyCode(request)) {
            attr.addFlashAttribute("msg", "验证码错误");
            return "redirect:/login.html";
        }


        User user = userService.login(mobile, password);
        if (user != null) {
            request.getSession().setAttribute("user", user);
            return "redirect:/dashboard.html";
        }

        attr.addFlashAttribute("msg", "用户名或者密码错误");

        return "redirect:/login.html";
    }

    /**
     * 用户注册
     *
     * @param request
     * @return
     */
    @PostMapping("/register")
    public String register(HttpServletRequest request,
                           RedirectAttributes attr) {

        String userName = request.getParameter("user_name");
        String password = request.getParameter("password");
        String rPassword = request.getParameter("confirm_password");
        String mobile = request.getParameter("mobile");

        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)
            || StringUtils.isEmpty(rPassword) || StringUtils.isEmpty(mobile)) {
            attr.addFlashAttribute("msg","输入的信息不全!");
            return  "redirect:/register.html";
        }

        if (!password.equals(rPassword)) {
            attr.addFlashAttribute("msg","密码和确认密码对不上!");
            return "redirect:/register.html";
        }

        User user = new User();
        user.setUserName(userName);
        user.setMobile(mobile);
        user.setPassword(password);

        int register = userService.register(user);
        if (register > 0) {
            return "redirect:/login.html";
        }

        return "register";
    }


    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "redirect:/";
    }
}
